package cn.bytepulse.web.admin.controller;

import cn.bytepulse.annotation.Log;
import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.web.admin.entity.SysMenu;
import cn.bytepulse.web.admin.service.SystemMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiejiebiezheyang
 * @since 2024-07-03 20:00
 */
@Tag(name = "管理-系统管理-菜单管理")
@RestController
@RequestMapping("/system/menu")
@RequiredArgsConstructor
public class SystemMenuController {

    private final SystemMenuService systemMenuService;

    @GetMapping("/userMenu")
    @Operation(summary = "当前用户菜单")
    @Log(value = "管理-系统管理-菜单管理-当前用户菜单", persist = true)
    public AjaxResult userMenu() {
        return systemMenuService.userMenu();
    }

    @GetMapping("/menuTree")
    @Operation(summary = "菜单列表")
    @Log(value = "管理-系统管理-菜单管理-菜单列表", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:query')")
    public AjaxResult topMenuList() {
        return systemMenuService.topMenuList();
    }

    @PostMapping("/addMenu")
    @Operation(summary = "添加菜单")
    @Log(value = "管理-系统管理-菜单管理-添加菜单", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:add')")
    public AjaxResult addMenu(@RequestBody @Validated SysMenu sysMenu) {
        return systemMenuService.addMenu(sysMenu);
    }

    @PutMapping("/updateMenu")
    @Operation(summary = "更新菜单")
    @Log(value = "管理-系统管理-菜单管理-更新菜单", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:update')")
    public AjaxResult updateMenu(@RequestBody @Validated SysMenu sysMenu) {
        return systemMenuService.updateMenu(sysMenu);
    }

    @DeleteMapping("/deleteMenu")
    @Operation(summary = "删除菜单")
    @Log(value = "管理-系统管理-菜单管理-删除菜单", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:delete')")
    public AjaxResult deleteMenu(@RequestParam Integer id) {
        return systemMenuService.deleteMenu(id);
    }

    @GetMapping("/permList")
    @Operation(summary = "权限列表")
    @Log(value = "管理-系统管理-菜单管理-权限列表", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:perm:query')")
    public AjaxResult permList(@RequestParam Integer id) {
        return systemMenuService.permList(id);
    }

    @PostMapping("/addPerm")
    @Operation(summary = "添加权限")
    @Log(value = "管理-系统管理-菜单管理-添加权限", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:perm:add')")
    public AjaxResult addPerm(@RequestBody @Validated(SysMenu.CreatePerm.class) SysMenu sysMenu) {
        return systemMenuService.addPerm(sysMenu);
    }

    @PostMapping("/updatePerm")
    @Operation(summary = "更新权限")
    @Log(value = "管理-系统管理-菜单管理-更新权限", persist = true)
    @PreAuthorize("hasAnyAuthority('system:menu:perm:update')")
    public AjaxResult updatePerm(@RequestBody SysMenu sysMenu) {
        return systemMenuService.updatePerm(sysMenu);
    }


    @GetMapping("/icons")
    @Operation(summary = "菜单图标")
    @Log(value = "管理-系统管理-菜单管理-菜单图标", persist = true)
    public AjaxResult icons() {
        return systemMenuService.icons();
    }
}
